package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.entity.EsDevHisWater;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.service.EsDevHisWaterService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHisWater;
import com.dykj.fireFighting.web.message.InsertDevHisWaterMessage;
import com.dykj.fireFighting.web.service.DevHisService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengbo
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisWaterMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisWaterMessage.TOPIC
)
public class InsertDevHisWaterConsumer implements RocketMQListener<InsertDevHisWaterMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisWaterConsumer.class);
    @Autowired
    private RedisService redisService;
    @Autowired
    private DevHisService devHisService;
    @DubboReference
    private EsDevHisWaterService esDevHisWaterService;
    @Override
    public void onMessage(InsertDevHisWaterMessage insertDevHisWaterMessage) {
        DevHisWater devHisWater = insertDevHisWaterMessage.getDevHisWater();
        LOGGER.info("===========插入EsDevHisWater数据到mysql==============");
        devHisService.insertDevHisWater(devHisWater);
        LOGGER.info("===========插入EsDevHisWater数据到ES==============");
        EsDevHisWater esDevHisWater = new EsDevHisWater();
        BeanUtils.copyProperties(devHisWater, esDevHisWater);
        esDevHisWater.setUnitId(devHisWater.getUnitId());
        esDevHisWater.setDeviceName(devHisWater.getDeviceName());
        esDevHisWater.setUniqueCode(devHisWater.getDevId());
        System.out.println("===="+esDevHisWater.getId());
        esDevHisWaterService.importAll(esDevHisWater);
        LOGGER.info("===========插入EsDevHisWater数据到ES:=============" + JSON.toJSONString(esDevHisWater));
        redisService.set(devHisWater.getId(), JSON.toJSONString(devHisWater), 3600L);
    }
}
